/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */
#include <stdlib.h>
#include "composer.h"
#include "type_id.h"
#include "types.h"
#include "unresolved_type.h"
LIST_HEAD(context_stack);   // struct context_st : a stack of contexts
struct context_st *context_fork_push() {
	// Fetch the closest context.
	struct context_st *innest_context = list_peek_entry(struct context_st, context_head_list, &context_stack),
			  *new_context = malloc(sizeof struct context_st);
	// Inserts a new, blank context in advance of it.
	list_add(&new_context->context_head_list, &context_stack);

	// Initializes the new context
	new_context->enclosing_class = innest_context!=NULL ? innest_context->enclosing_class : NULL;
	new_context->innest_search_entry = innest_context!=NULL ? innest_context->innest_search_entry : NULL;
	LIST_HEAD_INIT(new_context->search_context_item_head);

	// Fetches the items in the context.
	struct search_context_item_st *item;
	if(innest_context!=NULL)
		list_foreach_entry(item, struct search_context_item_st, search_context, &innest_context->search_context_item_head) {
			struct search_context_item_st *item_copy = malloc(sizeof(struct search_context_item_st));
			// Copies the items.
			item_copy->search_context_item_entry = item->search_context_item_entry;
			item_copy->search_context_type = item->search_context_type;
			// Add a new item to the new context, in the same order as items were in the outer context.
			list_add_tail(&item_copy->search_context, &new_context->search_context_item_head);
		}
	return new_context;
}

void *context_pop_free() {
	struct context_st *innest_context;
	list_pop_entry(innest_context, &context_stack, struct context_st, context_head_list);
}
struct context_st *context_peek() {
	list_peek_entry(struct context_st, context_head_list, &context_stack);
}
void context_free_all() {
	struct context_st *innest_context;
	do {
		list_pop_entry(innest_context, &context_stack, struct context_st, context_head_list);
	} while(innest_context!=NULL);
}
